{% extends 'base.html' %}
{% load helpers %}

{% block header %}
    {{ block.super }}
    {% if new_release %}
        {# new_release is set only if the current user is a superuser or staff member #}
        <div class="alert alert-info text-center" role="alert">
            <i class="mdi mdi-information-outline"></i>
            A new release is available: <a href="{{ new_release.url }}">Nautobot v{{ new_release.version }}</a> |
            <a href="https://nautobot.readthedocs.io/en/v{{ new_release.version }}/installation/upgrading/">Upgrade instructions</a>
        </div>
    {% endif %}
{% endblock %}


{% block content %}
    {% include 'search_form.html' %}
    <div class="row">
        <div class="col-sm-12">
            <div class="homepage_column" style="columns: 4 360px">
                {% for panel_name, panel_details in registry.homepage_layout.panels.items %}
                    {% if request.user|has_one_or_more_perms:panel_details.permissions or not "HIDE_RESTRICTED_UI"|settings_or_config %}
                        <div class="panel panel-default" style="break-inside: avoid" data-panel-weight="{{ panel_details.weight }}">
                            <div class="panel-heading">
                                <strong>{{ panel_name }}</strong>
                            </div>
                            <div class="list-group">
                                {% if  panel_details.rendered_html %}
                                    {% autoescape off %}{{ panel_details.rendered_html }}{% endautoescape %}
                                {% elif panel_details.items %}
                                    {% for item_name, item_details in panel_details.items.items %}
                                        {% if  item_details.rendered_html %}
                                            <div class="list-group-item" data-item-weight="{{ item_details.weight }}">
                                                {% if request.user|has_one_or_more_perms:item_details.permissions or not "HIDE_RESTRICTED_UI"|settings_or_config %}
                                                    {% autoescape off %}{{ item_details.rendered_html }}{% endautoescape %}
                                                {% endif %}
                                            </div>
                                        {% elif not item_details.items.items %}
                                            {% if request.user|has_one_or_more_perms:item_details.permissions or not "HIDE_RESTRICTED_UI"|settings_or_config %}
                                                <div class="list-group-item" data-item-weight="{{ item_details.weight }}">
                                                    {% if request.user|has_perms:item_details.permissions %}
                                                        <span class="badge pull-right">{{ item_details.count }}</span>
                                                        <h4 class="list-group-item-heading">
                                                            {% comment %}
                                                                Use 'url xxx as variable' so that an invalid
                                                                link doesn't throw a NoReverseMatch exception.
                                                            {% endcomment %}
                                                            {% url item_details.link as item_url %}
                                                            {% if item_url %}
                                                                <a href="{{ item_url }}">{{ item_name }}</a>
                                                            {% else %}
                                                                <a>ERROR: Invalid link!</a>
                                                            {% endif %}
                                                        </h4>
                                                    {% else %}
                                                        <span class="badge pull-right"><i class="mdi mdi-lock"></i></span>
                                                        <h4 class="list-group-item-heading">{{ item_name }}</h4>
                                                    {% endif %}
                                                    <p class="list-group-item-text text-muted">{{ item_details.description }}</p>
                                                </div>
                                            {% endif %}
                                        {% else %}
                                            <div class="list-group-item" data-item-weight="{{ item_details.weight }}">
                                                <h4 class="list-group-item-heading">{{ item_name }}</h4>
                                                {% for group_item_name, group_item_details in item_details.items.items %}
                                                    {% if request.user|has_one_or_more_perms:group_item_details.permissions or not "HIDE_RESTRICTED_UI"|settings_or_config %}
                                                        {% if request.user|has_perms:group_item_details.permissions %}
                                                            <span class="badge pull-right">{{ group_item_details.count }}</span>
                                                            <p style="padding-left: 20px;">
                                                                {% comment %}
                                                                    Use 'url xxx as variable' so that an invalid
                                                                    link doesn't throw a NoReverseMatch exception.
                                                                {% endcomment %}
                                                                {% url group_item_details.link as group_url %}
                                                                {% if group_url %}
                                                                    <a href="{{ group_url }}">{{ group_item_name }}</a>
                                                                {% else %}
                                                                    <a>ERROR: Invalid link!</a>
                                                                {% endif %}
                                                            </p>
                                                        {% else %}
                                                            <span class="badge pull-right"><i class="mdi mdi-lock"></i></span>
                                                            <p style="padding-left: 20px;">{{ group_item_name }}</p>
                                                        {% endif %}
                                                    {% endif %}
                                                {% endfor %}
                                            </div>
                                        {% endif %}
                                    {% endfor %}
                                {% endif %}
                            </div>
                        </div>
                    {% endif %}
                {% endfor %}
            </div>
        </div>
    </div>
{% endblock %}
